package com.sc.nft.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.BoxProductDao;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.IdcardInfoDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.IdcardInfoVO;
import com.sc.nft.enums.AddCollectionEnum;
import com.sc.nft.enums.CommodityType;
import com.sc.nft.enums.ReceivedCollectionType;
import com.sc.nft.service.BoxItemService;
import com.sc.nft.service.IdcardInfoService;
import com.sc.nft.service.ProductStockService;
import com.sc.nft.service.UserCollectionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 服务接口实现
 *
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-06-01 18:12:41
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class IdcardInfoServiceImpl implements IdcardInfoService {
    private final IdcardInfoDao idcardInfoDao;
    private final UserCollectionService userCollectionService;
    private final BoxItemService boxItemService;
    private final ProductStockService productStockService;
    private final BoxProductDao boxProductDao;
    private final DigitalCollectionDao digitalCollectionDao;

    @Override
    public Page<IdcardInfoVO> pageIdCard(Integer pageNo, Integer pageSize, String name, String userTel, String userEmail) {
        return idcardInfoDao.pageIdCard(pageNo, pageSize, name, userTel, userEmail);
    }


    /**
     * 添加到个人藏品
     *
     * @param digitalCollectionProductInfo
     * @param userId
     * @param inviteTask
     */
    private void addUserCollection(DigitalCollectionProductInfo digitalCollectionProductInfo, Long userId, InviteTask inviteTask, InviteProductTask inviteProductTask) {
        //插入用户拥有的藏品
        Long productId = digitalCollectionProductInfo.getId();
        Long collectionId = null;
        boolean isOpen = false;
        Long boxId = null;
        String boxTitle = null;
        String boxImage = null;
        //如果兑换的是盲盒
        productStockService.subIntegralStock(productId, digitalCollectionProductInfo.getRelationId(), null, 1, userId, CommodityType.INVITE_TASKS);
        switch (digitalCollectionProductInfo.getTypeClassify()) {
            case COLLECTION:
                isOpen = true;
                collectionId = digitalCollectionProductInfo.getRelationId();
                break;
            case BLIND_BOX:
                BoxItem boxItem = boxItemService.openBoxByInviteProbability(productId, digitalCollectionProductInfo.getRelationId(), userId);
                collectionId = boxItem.getCollectionId();
                //减去盲盒库存
                productStockService.subIntegralStock(productId, digitalCollectionProductInfo.getRelationId(), boxItem.getId(), 1, userId, CommodityType.INVITE_TASKS);
                BoxProduct boxProduct = boxProductDao.getById(digitalCollectionProductInfo.getRelationId());
                boxId = boxProduct.getId();
                boxTitle = boxProduct.getFirstTitle();
                boxImage = digitalCollectionProductInfo.getCreationContent();
                isOpen = false;
        }
        DigitalCollection digitalCollection = digitalCollectionDao.getById(collectionId);
        UserCollection userCollection = userCollectionService.addCollection(userId, null, digitalCollection.getId(), IdUtil.getSnowflakeNextIdStr(), digitalCollection.getCreationContent(), digitalCollection.getLevel(), digitalCollection.getFirstTitle(),
                digitalCollection.getUserCreatorId(), ReceivedCollectionType.INVITE_TASKS, boxId, boxTitle, boxImage, isOpen, productId, 0L, 0L, digitalCollectionProductInfo.getProductNum(), 0, null, null, AddCollectionEnum.STAIR, null, null,false);
        //添加记录
        UserReceiveInvite userReceiveInvite = new UserReceiveInvite();
        userReceiveInvite.setUserId(userId);
        userReceiveInvite.setProductId(digitalCollectionProductInfo.getId());
        userReceiveInvite.setInviteTaskId(inviteTask.getId());
        userReceiveInvite.setInvitateRewardName(inviteTask.getInvitateRewardName());
        userReceiveInvite.setCreateTime(new Date());
        userReceiveInvite.setFirstTitle(inviteProductTask.getFirstTitle());
        userReceiveInvite.setTypeClassify(inviteProductTask.getTypeClassify());
        userReceiveInvite.setCollectionUuid(userCollection.getCollectionUuid());
        userReceiveInvite.setUserCollectionId(userReceiveInvite.getId());
        userReceiveInvite.insert();

        //添加藏品流转记录
        CollectionCirculatingRecord collectionCirculatingRecord = new CollectionCirculatingRecord();
        collectionCirculatingRecord.setCommodityType(CommodityType.INVITE_TASKS);
        collectionCirculatingRecord.setTypeClassify(digitalCollectionProductInfo.getTypeClassify());
        collectionCirculatingRecord.setUserId(userId);
        collectionCirculatingRecord.setCollectionId(digitalCollection.getId());
        collectionCirculatingRecord.setReceivedCollectionType(ReceivedCollectionType.INVITE_TASKS);
        collectionCirculatingRecord.setInviteTaskId(inviteTask.getId());
        collectionCirculatingRecord.setUserReceiveInviteId(userReceiveInvite.getId());
        collectionCirculatingRecord.insert();
    }
}